//
//  YXVistorView.swift
//  微博YX
//
//  Created by apple on 15/11/23.
//  Copyright © 2015年 lyx. All rights reserved.
//

import UIKit
//定义协议
protocol YXVistorViewDelegate: NSObjectProtocol {
    
    func vistorViewRegistClick()
    
    func vistorViewLoginClick()
    
}

/// 访客视图
class YXVistorView: UIView {
    //    代理属性
    weak var vistorViewDelegate: YXVistorViewDelegate?
    
    
    // swift默认所有的view都能通过xib/storyboard 加载
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // MARK: - 按钮点击事件
    //    调用注册按钮的监听事件
    func registClick(){
        vistorViewDelegate?.vistorViewRegistClick()
    }
    //    调用登录按钮的监听事件
    func loginClick(){
        // ? 前面的变量有值才执行后面的代码,没有值就什么都不做
        vistorViewDelegate?.vistorViewLoginClick()
    }
    
    
    override init(frame: CGRect) {
        // super是要在设置普通的存储型属性的下面
        super.init(frame: frame)
        
        prepareUI()
    }
    
    /// MARK: - 设置访客视图内容
    func setupVistorView(imageName: String, message: String) {
        //        隐藏房子
        homeView.hidden = true
        
        iconView.image = UIImage(named: imageName)
        
        maessageLable.text = message
        
        self.sendSubviewToBack(coverView)
    }
    
    var isPause = false
    
    /// 创建动画
    func rotationAnimation() {
        //        创建动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //        设置参数
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.duration = 20
        //        开始动画
        iconView.layer.addAnimation(animation, forKey: "homeRotation")
        // 当不可见的时候,系统会认为动画完成,在完成的时候不要移除动画
        animation.removedOnCompletion = false
        
        
        isPause = true
    }
    
    ///    暂停旋转
    func pauseAnimation(){
        //
        if !isPause{
            return
        }
        let pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer:nil)
        //        设置动画速度为0
        iconView.layer.speed = 0
        //        设置动画偏移时间
        iconView.layer.timeOffset = pauseTime
        isPause = false
    }
    
    ///    开始旋转
    func resumeAnimation(){
        //        获取暂停时间
        let pauseTime = iconView.layer.timeOffset
        //        设置动画速度为1
        iconView.layer.speed = 1
        iconView.layer.timeOffset = 0
        iconView.layer.beginTime = 0
        let timeSincePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        iconView.layer.beginTime = timeSincePause
    }
    
    //    创建视图窗口
    func prepareUI() {
        // 设置背景
        backgroundColor = UIColor(white: 237.0 / 255, alpha: 1)
        //        添加子控件
        
        addSubview(iconView)
        addSubview(homeView)
        addSubview(coverView)
        addSubview(maessageLable)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 2 添加约束 autolayout
        // 注意: 要设置约束的view有父控件后才能设置它的自动布局约束
        // 关闭Autoresizing,不让它干扰autolayout的约束
        // 约束需要添加到View上面,如果不清楚添加到自身还是父控件,可以直接添加到父控件
        iconView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        maessageLable.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        //        添加约束
        /// iconView CenterX与父控件的CenterX重合
        // item: 要添加约束的view
        // attribute: 设置的约束
        // relatedBy: Equal
        // toItem: 参照的view
        // attribute: 参照的view的约束
        //        转轮
        //       iconView x
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy:NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //      iconView  y
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy:NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        //        小房子
        //       homeView x
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy:NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //      homeView  y
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy:NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        //        消息
        //       maessageLable x
        addConstraint(NSLayoutConstraint(item: maessageLable, attribute: NSLayoutAttribute.CenterX, relatedBy:NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //      maessageLable  y
        addConstraint(NSLayoutConstraint(item: maessageLable, attribute: NSLayoutAttribute.Top, relatedBy:NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        //       maessageLable 宽度
        addConstraint(NSLayoutConstraint(item: maessageLable, attribute: NSLayoutAttribute.Width, relatedBy:NSLayoutRelation.Equal, toItem:nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 226))
        //        注册按钮
        //       registerButton x
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy:NSLayoutRelation.Equal, toItem: maessageLable, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        
        //      registerButton  y
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy:NSLayoutRelation.Equal, toItem: maessageLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        //       registerButton 宽度
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy:NSLayoutRelation.Equal, toItem:nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
        //       registerButton 高度
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy:NSLayoutRelation.Equal, toItem:nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        //        登录按钮
        //       loginButton x
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy:NSLayoutRelation.Equal, toItem: maessageLable, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        //      loginButton  y
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy:NSLayoutRelation.Equal, toItem: maessageLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        //       loginButton 宽度
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy:NSLayoutRelation.Equal, toItem:nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
        //       loginButton 高度
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy:NSLayoutRelation.Equal, toItem:nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        //        遮盖图片
        // 左边
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        
        // 上边
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        // 右边
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        // 下边
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
    }
    
    
    
    //懒加载UI
    //    转轮
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //    小房子
    private lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //    标题
    private lazy var maessageLable: UILabel = {
        let lable = UILabel()
        lable.text = "关注一些人,看看有神马惊喜"
        lable.textColor = UIColor.lightGrayColor()
        lable.numberOfLines = 0;
        lable.sizeToFit()
        return lable
        }()
    //    注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        // 设置按钮的背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.sizeToFit()
        //        添加按钮点击事件
        button.addTarget(self, action:"registClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
        }()
    //    登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        // 设置按钮的背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.sizeToFit()
        //        添加按钮点击事件
        button.addTarget(self, action:"loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
        }()
    //    遮盖图片
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))

}
